package com.cjkj.hospital.service.impl;

import com.cjkj.common.constant.HttpStatus;
import com.cjkj.common.exception.ServiceException;
import com.cjkj.common.utils.DateUtils;
import com.cjkj.common.utils.uuid.IdUtils;
import com.cjkj.hospital.domain.THospitalDept;
import com.cjkj.hospital.domain.THospitalEquipment;
import com.cjkj.hospital.domain.THospitalInfo;
import com.cjkj.hospital.domain.THospitalUser;
import com.cjkj.hospital.domain.dto.HospitalDataReq;
import com.cjkj.hospital.domain.vo.HospitalDataVo;
import com.cjkj.hospital.domain.vo.HospitalExportVo;
import com.cjkj.hospital.mapper.THospitalDeptMapper;
import com.cjkj.hospital.mapper.THospitalEquipmentMapper;
import com.cjkj.hospital.mapper.THospitalInfoMapper;
import com.cjkj.hospital.mapper.THospitalUserMapper;
import com.cjkj.hospital.service.ITHospitalInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.cjkj.common.utils.SecurityUtils.getUsername;

/**
 * 医院信息Service业务层处理
 * 
 * @author cjkj
 * @date 2024-08-26
 */
@Service
public class THospitalInfoServiceImpl implements ITHospitalInfoService
{
    @Resource
    private THospitalInfoMapper tHospitalInfoMapper;

    @Resource
    private THospitalEquipmentMapper tHospitalEquipmentMapper;

    @Resource
    private THospitalDeptMapper tHospitalDeptMapper;

    @Resource
    private THospitalUserMapper tHospitalUserMapper;

    /**
     * 查询医院信息
     * 
     * @param hospitalId 医院信息主键
     * @return 医院信息
     */
    @Override
    public THospitalInfo selectTHospitalInfoByHospitalId(String hospitalId)
    {
        return tHospitalInfoMapper.selectTHospitalInfoByHospitalId(hospitalId);
    }

    /**
     * 查询医院信息列表
     * 
     * @param req 参数
     * @return 医院信息
     */
    @Override
    public List<HospitalDataVo> selectTHospitalInfoList(HospitalDataReq req)
    {
        return tHospitalInfoMapper.selectTHospitalInfoList(req);
    }

    @Override
    public List<HospitalExportVo> getExportInfoList(HospitalDataReq req) {
        return tHospitalInfoMapper.selectExportInfoList(req);
    }

    /**
     * 新增医院信息
     * 
     * @param tHospitalInfo 医院信息
     * @return 结果
     */
    @Override
    public int insertTHospitalInfo(THospitalInfo tHospitalInfo)
    {
        String username = getUsername();
        Date nowDate = DateUtils.getNowDate();

        tHospitalInfo.setHospitalId(IdUtils.fastSimpleUUID());
        tHospitalInfo.setCreateBy(username);
        tHospitalInfo.setCreateTime(nowDate);
        tHospitalInfo.setUpdateBy(username);
        tHospitalInfo.setUpdateTime(nowDate);
        return tHospitalInfoMapper.insertTHospitalInfo(tHospitalInfo);
    }

    /**
     * 修改医院信息
     * 
     * @param tHospitalInfo 医院信息
     * @return 结果
     */
    @Override
    public int updateTHospitalInfo(THospitalInfo tHospitalInfo)
    {
        tHospitalInfo.setUpdateBy(getUsername());
        tHospitalInfo.setUpdateTime(DateUtils.getNowDate());
        return tHospitalInfoMapper.updateTHospitalInfo(tHospitalInfo);
    }

    /**
     * 批量删除医院信息
     * 
     * @param hospitalIds 需要删除的医院信息主键
     * @return 结果
     */
    @Override
    public String deleteTHospitalInfoByHospitalIds(String[] hospitalIds)
    {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (String hospitalId : hospitalIds) {

            // 1、判断医院信息
            THospitalInfo tHospitalInfo = tHospitalInfoMapper.selectTHospitalInfoByHospitalId(hospitalId);
            if(tHospitalInfo == null) {
                failureNum ++;
                failureMsg.append("<br/>").append(failureNum).append("、医院ID ").append(hospitalId).append(" 不存在！");
                continue;
            }
            String hospitalName = tHospitalInfo.getHospitalName();

            // 2、判断是否有未回收的设备
            List<THospitalEquipment> equipments = tHospitalEquipmentMapper.selectByHospitalId(hospitalId);
            if(!equipments.isEmpty()) {
                failureNum ++;
                failureMsg.append("<br/>").append(failureNum).append("、医院 ").append(hospitalName).append(" 有未回收的设备！");
                continue;
            }
            // 3、判断是否有未删除的科室数据
            List<THospitalDept> depts = tHospitalDeptMapper.selectTHospitalDeptByHospitalId(hospitalId);
            if(!depts.isEmpty()) {
                failureNum ++;
                failureMsg.append("<br/>").append(failureNum).append("、医院 ").append(hospitalName).append(" 有未删除的科室信息！");
                continue;
            }
            // 4、判断是否有未删除的用户数据
            List<THospitalUser> users = tHospitalUserMapper.selectTHospitalUserByHospitalId(hospitalId);
            if(!users.isEmpty()) {
                failureNum ++;
                failureMsg.append("<br/>").append(failureNum).append("、医院 ").append(hospitalName).append(" 有未删除的用户信息！");
                continue;
            }

            // 5、删除医院信息
            int i = tHospitalInfoMapper.deleteTHospitalInfoByHospitalId(hospitalId);
            if (i > 0) {
                successNum ++;
                successMsg.append("<br/>" + successNum + "、医院 " + hospitalName + " 删除成功！");
            } else {
                failureNum ++;
                failureMsg.append("<br/>" + failureNum + "、医院 " + hospitalName + " 删除失败！");
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，共 " + failureNum + " 条数据删除失败，错误如下：");
            throw new ServiceException(failureMsg.toString(), HttpStatus.ERROR_PLUS);
        } else {
            successMsg.insert(0, "恭喜您，数据已全部删除成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Map<String, String>> getOptionData() {
        return tHospitalInfoMapper.selectHospitalOptionData();
    }
}
